<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    JSONObject jsonData = new JSONObject();
    String ids = Util.null2String(request.getParameter("ids"));
    String curdate = DateUtil.getCurrentDate();
    User user = HrmUserVarify.getUser(request, response);
    String userid = user.getUID() + "";
    RecordSet rs = new RecordSet();
    try {
        String strs[] = getDepartid(userid).split(",");
        rs.executeQuery("select count(*) from uf_xsddtz where id in (" + ids + ") and sfyts !=0");
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        if(count>0){
            String username = strs[0];
            String requestname = "请购单-" + username + "-" + curdate;
            String formid = "35";
            String workflowid = getActiveWorkflowid(formid);
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put("sqr", userid);
            requestMainMap.put("sqbm", strs[1]);
            requestMainMap.put("sqrq", curdate);
            requestMainMap.put("qgbm", strs[1]);
            String requestid = WorkflowCreateByRequestMap(userid + "", workflowid, requestname, "0", requestMainMap);
            jsonData.put("requestid", requestid);
            if (Util.getIntValue(requestid) > 0) {
                rs.executeQuery("select id from formtable_main_"+formid+" where requestid=?", requestid);
                rs.next();
                String mainid = Util.null2String(rs.getString(1));
                rs.executeQuery("select * from uf_xsddtz where id in (" + ids + ") and sfyts !=0");
                while (rs.next()) {
                    String id = Util.null2String(rs.getString("id"));
                    String ljbh = Util.null2String(rs.getString("ljbh"));
                    String cpxh = Util.null2String(rs.getString("cpxh"));
                    String ggfzxs = Util.null2String(rs.getString("ggfzxs"));
                    String ydjhrq = Util.null2String(rs.getString("ydjhrq"));
                    String xssl = Util.null2String(rs.getString("xssl"));
                    String xsdddh = Util.null2String(rs.getString("xsdddh"));
                    String mxid = Util.null2String(rs.getString("mxid"));
                    Object [] params = new Object[]{mainid,ljbh,cpxh,ggfzxs,xssl,ydjhrq,ydjhrq,ydjhrq,xsdddh,mxid};
                    rs.executeUpdate("insert into formtable_main_" + formid + "_dt1 (mainid,ljbh,pm,gg,dgl,jhrq,dcrq,dkrq,lydh,lyxc) " +
                            "values (?,?,?,?,?,?,?,?,?,?)",params);
                    rs.executeUpdate("update uf_xsddtz set sfyts=0 where id=?",id);
                }
            }
        }else {
            jsonData.put("requestid", "-1");
        }

        out.print(jsonData.toJSONString());
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<%!
    BaseBean baseBean = new BaseBean();

    public static String getDepartid(String userid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select lastname,departmentid from hrmresource where id=?", userid);
        rs.next();
        String lastname = Util.null2String(rs.getString("lastname"));
        String departmentid = Util.null2String(rs.getString("departmentid"));
        return lastname+","+departmentid;
    }

    /**
     *@Description: 获取活跃版本号的workflowid
     *@Param: [formid]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/9/6
     */
    public static String getActiveWorkflowid(String formid) {
        RecordSet rs = new RecordSet();
        String workflowid = "";
        String sql = "select id,activeversionid,version from workflow_base where abs(formid)=?";
        rs.executeQuery(sql, formid);
        int count = rs.getCounts();
        while (rs.next()) {
            workflowid = count <= 1 ? Util.null2String(rs.getString("id")) : Util.null2String(rs.getString("activeversionid"));
        }
        return workflowid;
    }

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog("-----创建流程传递的参数个数--------");
        baseBean.writeLog("creatorId:" + creatorId);
        baseBean.writeLog("workflowId:" + workflowId);
        baseBean.writeLog("requestName:" + requestName);
        baseBean.writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建人Id
        requestInfo.setWorkflowid(workflowId);//工作流Id
        requestInfo.setDescription(requestName);//请求标题
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            baseBean.writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        baseBean.writeLog("-----创建流程传递的参数个数p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                baseBean.writeLog("=======" + e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            baseBean.writeLog(sbf.toString());
        } catch (Exception e) {
            baseBean.writeLog("错误：" + e);
        }
        return requestid;
    }

%>
